<?php
declare(strict_types=1);
/**
 * Created by PhpStorm.
 *
 * Author : Traveler.
 * Time   : 2019/12/27 15:28
 * Company: Inner Mongolia Zhengzhi Network Technology Co., Ltd
 */

namespace app\admin;

use think\Model;

/**
 * 模型基础类
 */
abstract class BaseModel extends Model
{
    /**
     * 查询字段
     * @var array
     */
    protected $query;

    /**
     * 过滤参数
     * @param array $param  待过滤的参数
     * @param array $append 追加的查询条件
     * @return array
     */
    protected function filterParam(array $param = [], array $append = [])
    {
        //处理参数，page, limit
        $page = isset($param['page']) ? $param['page'] : 1;
        $limit = isset($param['limit']) ? $param['limit'] : 0;

        // 处理参数：批量查询where
        $where = [];
        foreach ($param as $k => $v) {
            if ('' != $v && 'page' != $k && 'limit' != $k) {
                // 这里处理条件类型
                // 目前仅支持[=, like]，可根据业务需求扩展
                if (in_array($k, array_keys($this->query))) {
                    $op        = $this->query[$k];
                    $condition = $op == 'like' ? '%'. $v . '%' : $v;
                    $where[] = [$k, $op, $condition];
                }
            }
        }

        // 追加查询条件
        foreach ($append as $w) {
            $where[] = $w;
        }

        return compact('page', 'limit', 'where');
    }
}